package kuzmenkov.oip.adapters.sun.jai;

import kuzmenkov.oip.adapters.ImageProcessingAdapter;
import kuzmenkov.oip.common.FileTool;

import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import java.awt.geom.AffineTransform;
import java.net.URL;
import java.util.logging.Logger;

/**
 * This class adapts JAI Affine Transformation to an UI of an image processing applet
 * User: Michael Kuzmenkov
 * Date: 26.03.2009
 * Time: 1:32:53
 */
public class AffineTransformationAdapter extends  ImageProcessingAdapter {
    private static final Logger log = Logger.getLogger(MedianFilteringAdapter.class.getName());

 
    /**
     * It takes eight parameters. First parameter is the path to the image (String).
     * The rest of parameters is listed below:
     * m00 - The x coordinate scale element
     * m10 - The y coordinate shear element
     * m01 - The x coordinate shear element
     * m11 - The y coordinate scale element
     * m02 - The x coordinate translate element
     * m12 - The y coordinate translate element
     * interpolation - Interpolation
     * It returns one result. This is the output image (String)
     * <p/>
     * In interpolation, each pixel in a destination image is located with
     * integer coordinates at a distinct point D in the image plane. The
     * geometric transform T identifies each destination pixel with a
     * corresponding point S in the source image. The interpolation can be:
     * 0 (INTERP_NEAREST) - Nearest-neighbor interpolation. Assigns to point D
     * in the destination image the value of the pixel nearest S in the source
     * image.
     * 1 (INTERP_BILINEAR) - Bilinear interpolation. Assigns to Point D in the
     * destination a value that is a bilinear function of the four pixels
     * nearest S in the source image.
     * 2 (INTERP_BICUBIC) - Bicubic interpolation. Assigns to point D in the
     * destination image a value that is a bicubic function of the 16 pixels
     * nearest S in the source image.
     * 3 (INTERP_BICUBIC_2) - Bicubic2 interpolation. Similar to Bicubic, but
     * uses a different polynomial function.
     *
     * @param params the input parameters
     * @return the path to the output image
     */
    public Object[] process(Object[] params) throws Exception {
        // Load the source image from a file.
        String path = (String) params[0];

        Float m00 = (Float) params[1];
        Float m10 = (Float) params[2];
        Float m01 = (Float) params[3];
        Float m11 = (Float) params[4];
        Float m02 = (Float) params[5];
        Float m12 = (Float) params[6];

        Integer _interpolation = (Integer) params[7];
        log.info("path=" + path + ", m00=" + m00 + ", m10=" + m10 +
                ", m01=" + m01 + ", m11=" + m11 + ", m02=" + m02 + ", m12=" + m12 +
                ", interpolation=" + _interpolation);

        FileTool ftool = FileTool.getInstance();
        path = ftool.relPathToAbs(path);
        log.info("modified path="+path);
        RenderedOp srcImage = JAI.create("fileload", path);
        log.info("srcImage=" + srcImage);
        

        int interpolation = 0;
        int num = _interpolation.intValue();
        switch (num) {
            case 0:
                interpolation = Interpolation.INTERP_NEAREST;
                break;
            case 1:
                interpolation = Interpolation.INTERP_BILINEAR;
                break;
            case 2:
                interpolation = Interpolation.INTERP_BICUBIC;
                break;
            case 3:
                interpolation = Interpolation.INTERP_BICUBIC_2;
                break;
            default:
                log.warning("The interpolation is wrong. The nearest-neighbor interpolation will be used");
                interpolation = Interpolation.INTERP_NEAREST;


        }

        AffineTransform atr = new AffineTransform(m00, m10, m01, m11, m02, m12);
        Interpolation interp = Interpolation.getInstance(interpolation);

        RenderedOp dstImage = JAI.create("affine", srcImage, atr, interp);



        String dstFileName = "1.png";
        String dstPath = ftool.getTempDir(getSessionId())+dstFileName;
        log.info("destination path="+dstPath);
        JAI.create("filestore", dstImage, dstPath, "PNG", null);

        String relDstPath = ftool.getRelativeTempDir(getSessionId())+dstFileName;
        log.info("relative destination path="+relDstPath);
        return new Object[]{relDstPath};
        
    }
}